<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/tags/struts-bean" prefix="bean"%>
<%@ taglib uri="/tags/struts-html" prefix="html"%>
<%@ taglib uri="/tags/struts-logic" prefix="logic"%>
<html:html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title></title>
	<META http-equiv=Pragma content=no-cache>
	<META http-equiv=Cache-Control content=no-cache>
	<META http-equiv=Expires content=0>
	<link rel="stylesheet"
		href="<%=request.getContextPath()%>/resource/css/style.css" type="text/css">
	<script language="javascript" type="text/javascript"
		src="<%=request.getContextPath()%>/resource/js/util.js"></script>
	<script language="javascript" type="text/javascript"
		src="<%=request.getContextPath()%>/style/scripts/util.js"></script>
	<script language="javascript" type="text/javascript"
		src="<%=request.getContextPath()%>/resource/js/AdyhUtil.js"></script>
	<script language="javascript" type="text/javascript"
		src="<%=request.getContextPath()%>/resource/js/Validator.js"></script>	
	<script language="javascript" type="text/javascript"
		src="<%=request.getContextPath()%>/resource/calendar/DCDA_C03.js"></script>
	<script type="text/javascript"
		src="<%=request.getContextPath()%>/style/ext/adapter/ext/ext-base.js"></script>
	<script type="text/javascript"
		src="<%=request.getContextPath()%>/style/ext/ext-all.js"></script>	
	<script language="javascript" type="text/javascript">
  /**保存*/
  function ShowAlert()
  {	
	ResetMe();
	var groups = document.getElementById("groups").value;
	var groupQty = document.getElementById("groupQty").value;
	var startDate = document.getElementById("startDate").value;
	var finishDate = document.getElementById("finishDate").value;	
	var description = document.getElementById("description").value;
	var quantity = document.getElementById("quantity").value;
	var temporaryQty = document.getElementById("temporaryQty").value;
	
	var objPrompt = document.getElementById("divPrompt");
    objPrompt.style.display = "block";
    objPrompt.innerHTML = "";
	if(groups.length==0||groupQty.length==0||startDate.length==0||finishDate.length==0){
		if(groups.length==0){
			objPrompt.innerHTML += "<li><bean:message key="adyh.groups"/> <bean:message key="errors.empty"/>";
			document.getElementById("groups").className = "MyControlError";
		}
		if(groupQty.length==0){
			objPrompt.innerHTML += "<li><bean:message key="F03.groupQty"/> <bean:message key="errors.empty"/>";
			document.getElementById("groupQty").className = "MyControlError";
		}
		if(startDate.length==0){			
			objPrompt.innerHTML += "<li><bean:message key="F03.beginDate"/> <bean:message key="errors.empty"/>";
			document.getElementById("startDate").className = "MyControlError";
		}		
		if(finishDate.length==0){
			objPrompt.innerHTML += "<li><bean:message key="F03.endDate"/> <bean:message key="errors.empty"/>";
			document.getElementById("finishDate").className = "MyControlError";
		}
	}else{
		//校验是否有重复子项存在
		if(CheckRepeat()==false){
			return false;
		}
		//校验是否各组数量之和大于总数量
		if(parseInt(quantity)<parseInt(groupQty)+parseInt(temporaryQty)+parseInt(totalCount)){
			alert("各组数量之和不能大于总数量！");
			document.getElementById("groupQty").value="";
			return false;
		}
		//增加一行值
		addRow();				
	}	
  }
/**重置*/
function ResetMe()
{
    document.getElementById("groups").className = "MyText";
	document.getElementById("groupQty").className = "MyText";
	document.getElementById("startDate").className = "MyTextReadOnly";	
	document.getElementById("finishDate").className = "MyText";
	
	var objPrompt = document.getElementById("divPrompt");
    objPrompt.style.display = "none";
	objPrompt.innerHTML = "";
}

function ShowFormTitle(strTitle)
{
	document.write("<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" style=\"height:26px; background-position: left bottom; background-image: url(../../resource/image/FormTitle_A_4.gif); background-repeat: repeat-x;\">");
	document.write("  <tr >");
	document.write("	<td style=\"width:5px; background-position: left bottom; background-image: url(../../resource/image/FormTitle_A_1.gif); background-repeat: no-repeat;\">&nbsp;&nbsp;&nbsp;</td>");
	document.write("	<td class=\"FormTitle\" style=\"width:100%; padding-left:5px; padding-top:3px; background-position: left bottom; background-image: url(../../resource/image/FormTitle_A_2.gif); background-repeat: no-repeat;\">");
	document.write("		<span id=\"spanFormTitle\" class=\"SpanTitle\">"+ strTitle +"</span>");
	document.write("	</td>");
	document.write("	<td style=\"width:5px; background-position: right bottom; background-image: url(../../resource/image/FormTitle_A_3.gif); background-repeat: no-repeat;\">&nbsp;&nbsp;&nbsp;</td>");
    document.write("  </tr>");
	document.write("</table>");
}

//显示隐藏区域
function AddOnce(strDivId)
{
	var objDiv = document.getElementById(strDivId);
	var objInnerDivs = objDiv.getElementsByTagName("div");
            for (var i=0; i<objInnerDivs.length; i++)
			{
			    if(objInnerDivs[i].className == "DivTail")
				{
				    objInnerDivs[i].style.display = "block";
				}
				if(objInnerDivs[i].className == "DivWrap")
				{
				    objInnerDivs[i].style.display = "block";
				}
			}
}
//隐藏被置显示区域,并清空数据


function DelOnce(strDivId)
{
	var objDiv = document.getElementById(strDivId);
	var objInnerDivs = objDiv.getElementsByTagName("div");
            for (var i=0; i<objInnerDivs.length; i++)
			{
			    if(objInnerDivs[i].className == "DivTail")
				{
				    objInnerDivs[i].style.display = "none";
				}
				if(objInnerDivs[i].className == "DivWrap")
				{
				    objInnerDivs[i].style.display = "none";
				}
			}
}
//增加主体信息并锁定


function AddMain()
  {	
	ResetMain();
	var manufacturingOrder = document.getElementById("manufacturingOrder").value;
	var modelName = document.getElementById("modelName").value;
	var quantity = document.getElementById("quantity").value;	
	var objPrompt = document.getElementById("divPrompt");
    objPrompt.style.display = "block";
    objPrompt.innerHTML = "";
	if(manufacturingOrder.length==0||modelName.length==0||quantity.length==0){
		if(manufacturingOrder.length==0){
			objPrompt.innerHTML += "<li><bean:message key="common.ManufacturingOrder"/><bean:message key="errors.empty"/>";
			document.getElementById("manufacturingOrder").className = "MyControlError";
		}
		if(modelName.length==0){
			objPrompt.innerHTML += "<li><bean:message key="adyh.model"/> <bean:message key="errors.empty"/>";
			document.getElementById("modelName").className = "MyControlError";
		}
		if(quantity.length==0){			
			objPrompt.innerHTML += "<li><bean:message key="F03.quantity"/><bean:message key="errors.empty"/>";
			document.getElementById("quantity").className = "MyControlError";
		}				
	}else{		
		//将输入框置为disable
		//document.getElementById("manufacturingOrder").disabled="disabled";
		//document.getElementById("modelName").disabled="disabled";
		//document.getElementById("quantity").disabled="disabled";
		
		document.getElementById("manufacturingOrder").readOnly="true";
		document.getElementById("modelName").readOnly="true";
		document.getElementById("quantity").readOnly="true";
		//显示隐藏区域
		AddOnce('divDlock_2');
		//切换按钮
		document.getElementById("addDetail").style.display="none";
		document.getElementById("delDetail").style.display="";
	}	
  }
/**重置主体信息*/
function ResetMain()
{
    document.getElementById("manufacturingOrder").className = "MyText";
	document.getElementById("modelName").className = "MyTextReadOnly";
	document.getElementById("quantity").className = "MyTextReadOnly";		
	
	var objPrompt = document.getElementById("divPrompt");
    objPrompt.style.display = "none";
	objPrompt.innerHTML = "";
}
//删除主体信息并解除锁定、清空隐藏区域信息


function ClearAll()
  {			
		//将输入框置为enable并清空值

		document.getElementById("manufacturingOrder").readOnly="";
		//清空详细信息的输入条件

		ClearIn();
		//清空隐藏区域信息
		delRows();
		//隐藏区域	
		DelOnce('divDlock_2');
		//切换按钮
		document.getElementById("addDetail").style.display="";
		document.getElementById("delDetail").style.display="none";
		
  }
//清空详细信息的输入条件


function ClearIn()
  {
		document.getElementById("groups").value="";
		document.getElementById("groupQty").value="";
		document.getElementById("finishDate").value="";
		document.getElementById("startDate").value="";
		document.getElementById("description").value="";
  } 
function onSave(){		
	if(count==0){
		alert("<bean:message key="F0301.addError"/>");
	}else{	
		document.getElementById("keystr").value = idstr;		
		var operationType = document.getElementById("operationType").value;
		operation(operationType);
	}
  }
//动态加载phasesList  
function FiltPhases()
  {  		
      	var productCode = document.getElementById("productCode").value;      	      
   		Ext.Ajax.request(
   		{
		url : 'F020106.do' , 
		params : {operationType:'query',productCode:productCode},
		method: 'POST',
		success: function ( result, request) 
			{
			var listArray = new Array()
			var obj= Ext.decode(result.responseText) ;
			listArray = obj.data.list;
			document.forms[0].phasesCode.length = 0;
 	        		for (i=0; i<listArray.length; i++){
           	document.forms[0].phasesCode.options[document.forms[0].phasesCode.length] = new Option(listArray[i][1], listArray[i][0]);			
    		}
			},	
		failure: function ( result, request) { 
			alert("<bean:message key="F020106.title"/>");
			} 
		}
		);
  }
  function initDate(){
  		
  		var manufacturingOrder = document.getElementById("manufacturingOrder").value;     
  		var finishDate = document.getElementById("finishDate").value; 	      
   		Ext.Ajax.request(
   		{
		url : 'F0301.do' , 
		params : {operationType:'ajax',id:'beginDate',manufacturingOrder:manufacturingOrder,finishDate:finishDate},
		method: 'POST',
		success: function ( result, request) 
			{
			var reValue = "";
			reValue = result.responseText;
			document.forms[0].startDate.value = reValue; 
   			
			},	
		failure: function ( result, request){ 
			alert("<bean:message key="F020106.title"/>");
			} 
		}
		);
  }
  //动态加载 modelName  quantity
  function initDetail()
  {  		
      	var manufacturingOrder = document.getElementById("manufacturingOrder").value;      	      
   		Ext.Ajax.request(
   		{
		url : 'F0301.do' , 
		params : {operationType:'ajax',id:'modelquantity',manufacturingOrder:manufacturingOrder},
		method: 'POST',
		success: function ( result, request) 
			{
			var reValue = "";
			var reValues = new Array();
			reValue = result.responseText;
			if(reValue ==""){
				alert("没有此工作号！");
				return;
			}
			reValues = reValue.split('*');
			document.forms[0].modelName.value = reValues[0]; 
   			document.forms[0].quantity.value = reValues[1];
   			document.forms[0].familyCode.value = reValues[2];
    		document.forms[0].temporaryQty.value= reValues[3];
			},	
		failure: function ( result, request) { 
			alert("<bean:message key="F020106.title"/>");
			} 
		}
		);
  }
//校验是否输入了重复的子项
function CheckRepeat()
  {
	var groups = document.getElementById("groups").value;		
		for(i=0;i<count;i++){			
			if(groups==document.getElementsByName("crow")[i].value){
				alert("<bean:message key="F0301.groupsError"/>");
				return false;
			}			
		}
	return true;
  }
//校验各组数量是否大于总数量
function judgeLength()
{	
	var quantity = document.getElementById("quantity").value;
	var groupsQty = document.getElementById("groupQty").value;
	if(parseInt(quantity)<parseInt(groupsQty)){
		alert("各组数量不能大于总数量！");
		document.getElementById("groupQty").value="";
	}
	
}  
</script>
</head>

<body>
<html:form method="post" action="/pages/F03/F0301.do">
<html:hidden property="operationType" />
<input type="hidden" name="keystr" />
<html:hidden property="errorFlag" />
<html:hidden property="familyCode" />
<html:hidden property="temporaryQty" />
<!--Begin：表单标题-->
<div class="DivBlock">
	<script language="javascript" type="text/javascript">
		ShowFormTitle("<bean:message key="F0301.title"/>");
	</script>
</div>
<!--End：表单标题-->
<!--Begin：表单主体区块-->
		<div class="DivBlock">
			<div class="DivWrap" style="display: block">
				<TABLE WIDTH="100%" BORDER="0" CELLSPACING="0" CELLPADDING="0">
					<TR>
						<TD ALIGN="right" NOWRAP><!--工作号--><bean:message key="common.ManufacturingOrder"/>&nbsp;</TD>
						<TD ALIGN="left" NOWRAP>
							<html:text property="manufacturingOrder" styleClass="MyText" onchange="javascript:initDetail();"/>
							<span class="Need">*</span>
						</TD>
						<TD ALIGN="right" NOWRAP><!--产品型号--><bean:message key="adyh.model"/>&nbsp;</TD>
						<TD ALIGN="left" NOWRAP>
							<html:text property="modelName" styleClass="MyTextReadOnly" readonly="true"/>
							<span class="Need">*</span>
						</TD>
						<TD ALIGN="right" NOWRAP><!--总数量--><bean:message key="F03.quantity"/>&nbsp;</TD>
						<TD ALIGN="left" NOWRAP>
							<html:text property="quantity" styleClass="MyTextReadOnly" readonly="true" style="text-align:right"/>
							<span class="Need">*</span>
						</TD>
						<TD ALIGN="left" NOWRAP>
							<input type="button" name="addDetail" class="MyButton"
								onClick="AddMain()" value="<bean:message key="common.add"/>">
							<input type="button" name="delDetail" class="MyButton" style="display:none"
								onClick="ClearAll()" value="<bean:message key="common.delete"/>">
						</TD>
					</TR>
				</TABLE>
			</div>
		</div>
		<!--End：表单主体区块-->
		<!--Begin：表单主体区块-->
		<div id="divDlock_2" class="DivBlock">
			<div class="DivWrap" style="display:none">
				<table width="100%" border="0" cellspacing="0" cellpadding="0">
					<tr>
						<td><strong><bean:message key="F0301.groupsList"/></strong></td>
					</tr>
				</table>
			</div>
			<div class="DivTail" style="display:none">
				<table width="100%" border="0" cellspacing="0" cellpadding="0">
					<TR>
						<TD ALIGN="right" NOWRAP><!--组号--><bean:message key="adyh.groups"/>&nbsp;</TD>
						<TD ALIGN="left" NOWRAP>
							<html:text property="groups" styleClass="MyText" />
							<span class="Need">*</span>
						</TD>
						<TD ALIGN="right" NOWRAP><!--数量--><bean:message key="F03.groupQty"/>&nbsp;</TD>
						<TD ALIGN="left" NOWRAP>
							<html:text property="groupQty" styleClass="MyText" style="text-align:right" onchange="javascript:judgeLength()"/>
							<span class="Need">*</span>
						</TD>
						<TD ALIGN="right" NOWRAP><!--结束时间--><bean:message key="F03.endDate"/>&nbsp;</TD>
						<TD ALIGN="left" NOWRAP>
							<html:text property="finishDate" styleClass="MyText" onblur="javascript:initDate();" onclick="return CalendarClick(this)" />
							&nbsp;
							<span class="Need">*</span>
						</TD>
						<TD ALIGN="left" NOWRAP>
							<input type="button" name="add" class="MyButton"
								onClick="ShowAlert();ClearIn();" value="<bean:message key="common.add"/>">							
						</TD>
					</TR>
					<TR>
						<TD ALIGN="right" NOWRAP><!--备注--><bean:message key="adyh.remark"/>&nbsp;</TD>
						<TD ALIGN="left" NOWRAP colspan="3">
							<html:text property="description" styleClass="MyText" style="width:86%" />							
						</TD>
						<TD ALIGN="right" NOWRAP><!--开始时间--><bean:message key="F03.beginDate"/>&nbsp;</TD>
						<TD ALIGN="left" NOWRAP>
							<html:text property="startDate" styleClass="MyTextReadOnly" readonly="true" />
						</TD>
						<TD ALIGN="left" NOWRAP>
								&nbsp;					
						</TD>
					</TR>
				</table>
			</div>								
			<div class="DivTail" style="display:none">
				<table id="dytb" width="100%" cellpadding="1" cellspacing="1"
					border="0" rules="none"  class="ListBackground">
					<tbody id="tb1">
					<TR class="ListTitle" id="title">
						<TD ALIGN="center" NOWRAP><!--组号--><bean:message key="adyh.groups"/></TD>
						<TD ALIGN="right" NOWRAP><!--数量--><bean:message key="F03.groupQty"/></TD>
						<TD ALIGN="center" NOWRAP><!--开始时间--><bean:message key="F03.beginDate"/></TD>
						<TD ALIGN="center" NOWRAP><!--结束时间--><bean:message key="F03.endDate"/></TD>
						<TD ALIGN="center" NOWRAP><!--备注--><bean:message key="adyh.remark"/></TD>
						<TD ALIGN="center" NOWRAP><!--操作--><bean:message key="common.operate"/></TD>
					</TR>					
					</tbody>
				</table>
			</div>						
		</div>
		<!--End：表单主体区块-->			
		
		<!--Begin：表单按钮及提示区-->
		<div class="DivBlock">
			<div class="DivButton">
				<input type="button" name="Submit1" class="MyButton" onClick="onSave()" value="<bean:message key="common.save"/>">
				<input type="button" name="Submit2" class="MyButton" value="<bean:message key="common.back"/>"
					onClick="javascript:operation('back');">
			</div>
			<div class="DivPrompt" id="divPrompt" style="display: none"></div>
		</div>
		<!--End：表单按钮及提示区-->
	</html:form>
</body>
</html:html>
<script language="javascript" type="text/javascript">
InitForm();
//动态增加行
var itable = document.getElementById("tb1");//表格
var key = itable.rows.length;//id起始值


var count = 0;
var totalCount =0;
var idstr = "";
function addRow(){//添加表格的一行	
	var currRowNum = itable.rows.length;//当前位置
		
	var iTR;//插入的行

	var iTD;//插入的列
		
	iTR=itable.insertRow(currRowNum);
	
	idstr = idstr+(key+",");//组装id串(1,2,3....)
	
	iTR.id="tr"+key;//行id
	iTR.className="ListRow1";		
	
	iTD=iTR.insertCell(0);	
	var id1=document.getElementById("groups").value;
	iTD.innerHTML="<td><input id=\"crow"+key+"\" name=\"crow\" value=\""+id1+"\" styleClass=\"MyText\" style=\"width:100%;text-align:center;background:none;border:0pt\" readonly=\"true\"/></td>";		
			
	iTD=iTR.insertCell(1);			
	var id2=document.getElementById("groupQty").value;
	iTD.innerHTML="<td><input id=\"frow"+key+"\" name=\"frow\" value=\""+id2+"\" styleClass=\"MyText\" style=\"width:100%;text-align:center;background:none;border:0pt\" readonly=\"true\" /></td>";
	
	iTD=iTR.insertCell(2);		
	var id3=document.getElementById("startDate").value;
	iTD.innerHTML="<td><input id=\"prow"+key+"\" name=\"prow\" value=\""+id3+"\" styleClass=\"MyText\" style=\"width:100%;text-align:center;background:none;border:0pt\" readonly=\"true\" /></td>";
	
	iTD=iTR.insertCell(3);		
	var id4=document.getElementById("finishDate").value;
	iTD.innerHTML="<td><input id=\"brow"+key+"\" name=\"brow\" value=\""+id4+"\" styleClass=\"MyText\" style=\"width:100%;text-align:center;background:none;border:0pt\" readonly=\"true\" /></td>";
	
	iTD=iTR.insertCell(4);	
	var id5=document.getElementById("description").value;
	iTD.innerHTML="<td><input id=\"mrow"+key+"\" name=\"mrow\" value=\""+id5+"\" styleClass=\"MyText\" style=\"width:100%;text-align:center;background:none;border:0pt\" readonly=\"true\" /></td>";
	
	iTD=iTR.insertCell(5);	
	iTD.innerHTML="<align=\"left\"><button onclick=\"delRow(tr"+key+","+id2+")\" class=\"Mybutton\"\"><bean:message key="common.delete"/></button>";
	iTD.className="list_td";	
	
	key++;
	count++;
	totalCount=parseInt(totalCount)+parseInt(id2);	
	
	
}

function delRow(str,id2)
{	
	if(confirm("确定要删除此行吗?")){
	
	sIndex=str.rowIndex;
 	if(sIndex=='')
    	return;
 
	itable.deleteRow(sIndex);
	count--;
	totalCount = parseInt(totalCount)-parseInt(id2);
	
	replaceIdStr(str.id);
	return true;
	}else{
		return false;
	}
}

function delRows()
{			
 	for(i=0;i<count;) {   	
   	itable.deleteRow(count);
   	count--;
  	} 	 		  	
}

function replaceIdStr(str)
{
	if(str=="")
	{
		return ;
	}else
	{
		str = str.replace("tr","");//去掉id中的“tr”部分tr2-->2
		str = str+",";
		
		idstr = idstr.replace(str,"");
		
	}
}
</script>